//
//  HomeGoodsCollectionCell.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/5.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "HomeGoodsCollectionCell.h"
#import "HomeGoodsCollectItemCell.h"

@interface HomeGoodsCollectionCell () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation HomeGoodsCollectionCell

+ (CGFloat)getHeightWithArr:(NSArray *)arr {
    if (arr.count == 0) {
        return 0;
    } else {
        return 82 + 10 + 100;
    }
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configure];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configure];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.collectionView setMj_insetL:10];
    [self.collectionView setMj_insetR:10];
    self.collectionView.frame = CGRectMake(0, 10, self.width, self.height - 20);

}

#pragma mark - Method
- (void)configure {
    [self.contentView addSubview:self.collectionView];
    _dataArr = [NSMutableArray array];
}

- (void)setValueForCellWithListArr:(NSMutableArray <HomeGoodsItem *> *)arr {
    _dataArr = [NSMutableArray arrayWithArray:arr];
    [self.collectionView reloadData];
    self.collectionView.hidden = (arr.count == 0);
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeGoodsItem *item = [self.dataArr objectAtIndex:indexPath.row];
    HomeGoodsCollectItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.iconImageView.backgroundColor = [UIColor imageEDBackgroundColor];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:item.gimg] placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
    cell.titleLabel.text = item.gname;
    if (item.isigoods) {
        cell.priceLabel.text = [NSString stringWithFormat:@"%@积分", item.gintegral];
        cell.oldLabel.text = @"";
    } else {
        cell.priceLabel.text = [NSString stringWithFormat:@"¥%@", item.gprice];
        cell.oldLabel.text = [NSString stringWithFormat:@"¥%@", item.goldprice];
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(100, [HomeGoodsCollectionCell getHeightWithArr:self.dataArr]);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (self.block) {
        self.block(indexPath.row);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 100;
//}

#pragma mark - Getters
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(100, [HomeGoodsCollectionCell getHeightWithArr:self.dataArr]);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 横向
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = NO;
        _collectionView.alwaysBounceHorizontal = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeGoodsCollectItemCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}

@end
